package org.software.views.student;

import org.software.controller.StudentController;
import org.software.dao.GroupDataProvider;
import org.software.views.MenuFrame;

import javax.swing.*;
import java.awt.*;

public class StudentFrame extends JFrame {
    public StudentFrame() {
        setTitle("新增学生");
        setSize(600, 400);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridLayout(2, 3));
        JLabel nameLable = new JLabel("姓名:");
        JTextField nameField = new JTextField(15);

        JLabel idLable = new JLabel("学号:");
        JTextField idField = new JTextField(15);

        JLabel groupNameLabel = new JLabel("小组名称：");
        JTextField groupNameField = new JTextField(15);

        JButton addButton = new JButton("新增");
        JButton backButton = new JButton("返回");
        setLayout(new FlowLayout(FlowLayout.CENTER));
        add(nameLable);
        add(nameField);
        add(idLable);
        add(idField);
        add(groupNameLabel);
        add(groupNameField);
        add(addButton);
        add(backButton);

        addButton.addActionListener(e -> {
            String studentId = idField.getText().trim();
            String name = nameField.getText().trim();
            String groupName = groupNameField.getText().trim();
            if (studentId.isEmpty()|| name.isEmpty()) {
                JOptionPane.showMessageDialog(StudentFrame.this,"学号和姓名不能为空","警告",JOptionPane.WARNING_MESSAGE);

            }else {
                StudentController studentController=new StudentController();
                boolean isAdded = studentController.studentAdd(studentId,name,groupName);
                if (isAdded) {
                JOptionPane.showMessageDialog(StudentFrame.this,"学生添加成功！");
                    GroupDataProvider.incrementStudentCountForGroup(groupName);
            }
            }

        });
            backButton.addActionListener(e ->
            {   dispose();
                new MenuFrame();
            });
            setVisible(true);
    }
}
